/**
 * 页面定制公共js
 */

import PublicApi from "@/common/api/form/public.js"
import AppUtil from "../../../../../assets/js/AppUtil";
import FormApi from "@/common/api/form/FormApi.js";

export default {
    props: {
        config:{
            type:Object,
            default:{}
        },
        viewKey:{
            type:String,
            default:""
        },
        localParams:{
            type:Object,
            default:{}
        }
    },
    data() {
        return {
            isViewRoot:true
        }
    },
    created() {
    },
    methods: {
        getCustomViewComponent(id,ref){
            return this.getComponent(id,ref);
        },
        /**
         * 获取组件对象
         * @param id
         * @param ref
         * @returns {*}
         */
        getComponent(id,ref) {
            var self=this;
            while(!self.isCustomView){
                self = self.$parent;
            }
            let component;
            this.callbackComponent(self.$refs,
                function (refKey, refComponent) {
                    if (id == refKey) {
                        component = refComponent;
                    }
            })
            if(component){
                this.callbackComponent(component.$refs,
                    function (refKey, refComponent) {
                        if (ref == refKey) {
                            component = refComponent;
                        }
                    })
            }
            return component;
        },
        callbackComponent(refs,callback){
            if(!refs){
                return;
            }
            for(let refKey of Object.keys(refs)){
                let refComponent = refs[refKey];
                if(refComponent) {
                    if (callback) {
                        if(refComponent instanceof Array && refComponent.length==1){
                            callback(refKey, refComponent[0]);
                            refComponent = refComponent[0];
                        }else{
                            callback(refKey, refComponent);
                        }
                    }
                    this.callbackComponent(refComponent.$refs, callback);
                }
            }
        },
        invokeScript(alias, params, callback) {
            FormApi.invoke(alias, params).then(res => {
                if(callback) {
                    callback(res);
                }
            })
        },
        invokeScriptPromise(alias,params){
            return FormApi.invoke(alias, params);
        },
        invokeCustomQueryPromise(alias,params){
            return FormApi.queryForJson(alias, {params: JSON.stringify(params)});
        },
        invokeCustomQuery(alias,params,callback){
            FormApi.queryForJson(alias, {params: JSON.stringify(params)})
            .then(res=>{
                if(res.success && callback) {
                    callback(res.data);
                }
            })
        },
        //根据参数名获取值
        getValueByParamName(paramName){
            var param = this.getRouterParams();
            if(param && param[paramName]){
                return param[paramName];
            }else {
                return null;
            }
        },
        //获取路由传递的参数
        getRouterParams(){
            return this.localParams
        },
        //根据配置获取参数
        getParams(mapping,callback){
            PublicApi.getParams(JSON.stringify(mapping)).then(res=>{
                if(callback){
                    callback(res);
                }
            }).catch(err=>{
                if(callback){
                    callback({});
                }
            })
        },
        /**
         * 路由懒加载
         * @param view
         * @returns {*}
         */
        loadView(view){
            return AppUtil.loadComponent(view);
        },
        getImage(url) {
            return import.meta.env.JPAAS_BASE_URL + url;
        },
    }
}
